//
//  AddressManagerViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/7/25.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "AddressManagerViewController.h"

#import "AddressTableViewCell.h"//地址Cell
#import "AddressModel.h"//地址Model

#import "AddNewAddressViewController.h"//添加新地址Controller

@interface AddressManagerViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation AddressManagerViewController
{
    /**地址数组*/
    NSMutableArray *_addressArray;
    
    CGFloat _cellHeight;
}

 static NSString *const AddressTableViewCellID = @"AddressTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _addressArray = [NSMutableArray array];
    NSArray *arr = @[@{@"name": @"123",
                       @"phone":@"15757575757",
                       @"address":@"浙江省 杭州市 西湖科技大厦D座南楼4楼 小橙网络",
                       @"defaults":@"0"},
                     @{@"name":@"134343",
                       @"phone":@"15757575757",
                       @"address":@"浙江省 杭州市 西湖科技大厦D座南楼4楼 小橙网络",
                       @"defaults":@"0"},
                     @{@"name":@"啦啦啦",
                       @"phone":@"15757575757",
                       @"address":@"浙江省 杭州市 西湖科技大厦D座南楼4楼 小橙网络哈哈哈哈哈哈",
                       @"defaults":@"0"}];
    for (NSDictionary *dic in arr) {
        AddressModel *model = [[AddressModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_addressArray addObject:model];
    }
    
    //设置导航栏
    [self setNav];
    
    //添加新地址
    [self addNewAddress];
    
    //注册TableView
    [self registerTableView];
}
- (void)setNav{
    self.title = @"管理收货地址";
    UIBarButtonItem *nextBack = [[UIBarButtonItem alloc] init];
    nextBack.title = @"";
    self.navigationItem.backBarButtonItem = nextBack;
}
#pragma mark === 添加新地址 ===
- (void)addNewAddress{
    UIButton *addNewAddress = [UIButton buttonWithType:UIButtonTypeCustom];
    addNewAddress.frame = CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50);
    addNewAddress.backgroundColor = [UIColor lightGrayColor];
    [addNewAddress setTitle:@"添加新地址" forState:UIControlStateNormal];
    addNewAddress.titleLabel.font = [UIFont systemFontOfSize:16];
    [addNewAddress addTarget:self action:@selector(jumpToAddNewAddress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addNewAddress];
}

#pragma mark === 注册TableView ===
- (void)registerTableView{
    [self.tableView registerClass:[AddressTableViewCell class] forCellReuseIdentifier:AddressTableViewCellID];
}

#pragma mark === 注册TableView ===
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _addressArray.count;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _cellHeight + 10;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AddressTableViewCellID];
    
    if (!cell) {
        cell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddressTableViewCellID];
    }
    AddressModel *model = _addressArray[indexPath.row];
    [cell setView:model];
    _cellHeight = cell.height;
    
    //cell点击事件
    cell.addressClick = ^(int temp){
        if (temp == 0) {
            for (AddressModel *tempModel in _addressArray) {
                if (model == tempModel) {
                     tempModel.defaults = @"1";
                }else tempModel.defaults = @"0";
            }
            [_tableView reloadData];
        }else if (temp == 1){
            NSLog(@"编辑");
        }else [self showAlert:@"确认删除地址?" andIndex:indexPath.row];
    };
    cell.selectionStyle = UITableViewCellStyleDefault;
    return cell;
}
//设置cell可编辑
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


#pragma mark === 删除地址 弹出警示框 ===
- (void)showAlert:(NSString *)string andIndex:(NSUInteger)index{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@", string] message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *done) {
        [_addressArray removeObjectAtIndex:index];
        [_tableView reloadData];
    }];
    [alert addAction:done];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark === 跳转到添加新地址 ===
- (void)jumpToAddNewAddress{
    AddNewAddressViewController *addNew = [[AddNewAddressViewController alloc] init];
    [self.navigationController pushViewController:addNew animated:YES];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50)style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = BackgroundCOLOR;
        _tableView.backgroundColor = BackgroundCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
@end
